require 'test/unit'
require '../lib/vector'

class TestVector < Test::Unit::TestCase
  def test_add_word_index
    vector = Vector.new
    
    vector.add_word_index(10)
    assert_equal(vector[10], 1)
    assert_equal(vector.num_words, 1)
    assert_equal(vector.max_word_index, 10)
    assert_equal(vector.min_word_index, 10)
    
    vector.add_word_index(10)
    assert_equal(vector[10], 2)    
    assert_equal(vector.num_words, 1)
    assert_equal(vector.max_word_index, 10)
    assert_equal(vector.min_word_index, 10)
    
    vector.add_word_index(1)
    assert_equal(vector[1], 1)    
    assert_equal(vector.num_words, 2)
    assert_equal(vector.max_word_index, 10)
    assert_equal(vector.min_word_index, 1)          
    
    
    vector.add_word_index(20)
    assert_equal(vector.max_word_index, 20)
    assert_equal(vector.min_word_index, 1)
  end
  
  def test_dot
    vector1 = Vector.new
    vector2 = Vector.new
    
    vector1.add_word_index(0)
    vector1.add_word_index(2)
    vector1.add_word_index(2)
    vector1.add_word_index(4)
    
    vector2.add_word_index(1)
    vector2.add_word_index(1)
    vector2.add_word_index(2)
    vector2.add_word_index(4)
    vector2.add_word_index(5)
    vector2.add_word_index(5)
    
    # v1 = [1, 0, 2, 0, 1, 0]
    # v2 = [0, 2, 1, 0, 1, 2]
    # v1 . v2 = 2 + 1 = 3
      
    assert_equal(vector1.dot(vector2), 3)    
  end
  
  def test_length
    vector = Vector.new
    
    vector.add_word_index(0)
    vector.add_word_index(2)
    vector.add_word_index(2)
    vector.add_word_index(4)
    
    assert_equal(vector.length, Math.sqrt(6))
  end
  
  def test_distance
    vector1 = Vector.new
    vector2 = Vector.new
    
    vector1.add_word_index(0)
    vector1.add_word_index(2)
    vector1.add_word_index(2)
    vector1.add_word_index(4)
    
    vector2.add_word_index(1)
    vector2.add_word_index(1)
    vector2.add_word_index(2)
    vector2.add_word_index(4)
    vector2.add_word_index(5)
    vector2.add_word_index(5)
    
    puts vector1.distance(vector2)
  end
end